/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.auth.login;

import com.arisgo.cloud.auth.login.granter.GranterStore;
import com.arisgo.cloud.auth.login.granter.ILoginGranter;
import com.arisgo.cloud.auth.login.model.Login;
import com.arisgo.cloud.auth.sys.entity.User;
import com.arisgo.cloud.common.Result;
import com.arisgo.cloud.core.redis.UserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author Arisgo
 * @since 2024/5/28
 */
@RestController
@AllArgsConstructor
@Tag(name = "登录 Api")
public class LoginApi {

    @PostMapping("/login")
    @Operation(summary = "登录")
    public Result<Map<Object, Object>> login(@RequestBody Login login) {
        ILoginGranter loginGranter = GranterStore.get(login.getType().name());
        return Result.success(loginGranter.login(login));
    }

    @GetMapping("/logout")
    @Operation(summary = "登出")
    public Result<Boolean> logout(@RequestParam(value = "userId") Long userId) {
        ILoginGranter loginGranter = GranterStore.getDefault();
        return Result.success(loginGranter.logout(userId));
    }

    @PostMapping("/register")
    @Operation(summary = "注册")
    public Result<Boolean> register(@RequestBody Login login) {
        ILoginGranter loginGranter = GranterStore.get(login.getType().name());
        return Result.success(loginGranter.register(login));
    }

    @GetMapping("/captcha")
    @Operation(summary = "验证码")
    public Result<Map<String, Object>> captcha() {
        ILoginGranter loginGranter = GranterStore.getDefault();
        return Result.success(loginGranter.captcha());
    }

    @GetMapping("/token/refresh")
    @Operation(summary = "续签token")
    public Result<String> refreshToken(@RequestParam(value = "refreshToken") String refreshToken) {
        ILoginGranter loginGranter = GranterStore.getDefault();
        return Result.success(loginGranter.refreshToken(refreshToken));
    }

}

